package com.katze.boot.plugins.shiro.authentication;

import com.katze.boot.common.domain.GlobalConstant;
import com.katze.boot.common.utils.EncryptUtils;
import com.katze.common.DatePattern;
import lombok.Data;
import org.apache.shiro.authc.AuthenticationToken;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * JSON Web Token
 */
@Data
public class JWTToken implements AuthenticationToken {

    private static final long serialVersionUID = 1282057025599826155L;

    private String token;

    private String exipreAt;

    private final EncryptUtils instance = EncryptUtils.getInstance(GlobalConstant.ACCESS_TOKEN_HASH_CACHE_PREFIX);

    public JWTToken(String token, boolean isDecrypt) {
        if (isDecrypt){
            this.token = instance.decrypt(token);
        } else {
            this.token = instance.encrypt(token);
            LocalDateTime expireTime = LocalDateTime.now().plus(JWTUtils.EXPIRE_TIME, ChronoUnit.MILLIS);
            this.exipreAt = DatePattern.localDateTime(expireTime);
        }
    }

    @Override
    public Object getPrincipal() {
        return token;
    }

    @Override
    public Object getCredentials() {
        return token;
    }

}
